//
//  BaseXmlParser.m
//  BosidengIphone
//
//  Created by  on 11-12-26.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "BaseXmlParser.h"

@implementation BaseXmlParser

// Property

-( id )init{
    
    if ( self =[ super init ]){
        
        _root =[[ NSMutableDictionary alloc ] init ];
        
    }
    
    return self ;
    
}

-( void )dealloc{
    
    [ _root release ], _root = nil ;
    
    [ super dealloc ];
    
}

// 一个便利方法，用于获取元素的属性值

-( NSDictionary *)getAtributes:( const xmlChar **)attributes withSize:( int )nb_attributes{
    
    NSMutableDictionary * atts=[[[ NSMutableDictionary alloc ] init ] autorelease];
    
    NSString *key,*val;
    
    for ( int i= 0 ; i<nb_attributes; i++){
        
        key = [ NSString stringWithCString :( const char *)attributes[ 0 ] encoding : NSUTF8StringEncoding ];
        
        val = [[ NSString alloc ] initWithBytes :( const void *)attributes[ 3 ] length :(attributes[ 4 ] - attributes[ 3 ]) encoding : NSUTF8StringEncoding ];
        
        [atts setObject :val forKey :key];
        
        //[key release ];
        [val release ];
        
        attributes += 5 ; // 指针移动 5 个字符串，到下一个属性
        
    }
    
    return atts;
    
}



//--------------------------------------------------------------//

#pragma mark -- libxml handler ，主要是 3 个回调方法 , 空方法，等待子类实现 --

//--------------------------------------------------------------//

// 解析元素开始标记时触发 , 在这里取元素的属性值

- ( void )startElementLocalName:( const xmlChar *)localname

                         prefix:( const xmlChar *)prefix

                            URI:( const xmlChar *)URI

                  nb_namespaces:( int )nb_namespaces

                     namespaces:( const xmlChar **)namespaces

                  nb_attributes:( int )nb_attributes

                   nb_defaulted:( int )nb_defaultedslo

                     attributes:( const xmlChar **)attributes

{
    
}

// 解析元素结束标记时触发

- ( void )endElementLocalName:( const xmlChar *)localname

                       prefix:( const xmlChar *)prefix URI:( const xmlChar *)URI

{
    
}

// 解析元素体时触发

- ( void )charactersFound:( const xmlChar *)ch

                      len:( int )len

{
    
}

// 返回解析结果

-( NSDictionary *)getResult{
    
    return _root ;
    
}

@end
